package com.lianniu.mgr.baseService.area.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.area.model.AdminAreaVo;
import com.lianniu.mgr.baseService.area.service.IAdminAreaService;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.util.StringUtil;




/**
 * @ClassName：AreaManageController
 * @Description:区域管理控制层(只做数据传输用)
 * @Since: 2016-03-23
 * @Version:1.0
 */
@Controller
@RequestMapping("/areaManager")
public class AreaRestController {
    final static String PROVINCE_LEVEL="2";

    private final static Logger logger = Logger.getLogger(AreaRestController.class);
    @Autowired
    private IAdminAreaService adminAreaService;

    @RequestMapping(value={"/initAdminAreaTree"},method={RequestMethod.POST,RequestMethod.GET})
    public @ResponseBody BaseResult test(HttpServletRequest request){
        logger.info("findAllArea begin");
        @SuppressWarnings("unchecked")
        String areaId = request.getParameter("areaId");
        if(StringUtil.isBlank(areaId)){
            areaId = "0";
        }
        List<AdminAreaVo> list = (List<AdminAreaVo>)adminAreaService.getByParentId(areaId).getData();
        logger.info("findAllArea end");
        if(null!=list && list.size()>0){
            for(int i=0;i<list.size();i++){
                AdminAreaVo vo = list.get(i);
                if(vo.getAreaCode()==null){
                    vo.setAreaCode("");
                }
                if(vo.getAreaDesc()==null){
                    vo.setAreaDesc("");
                }
            }
        }
        return BaseResult.success(list);
    }

    @RequestMapping(value={"/addAdminTreeStep1"},method={RequestMethod.POST,RequestMethod.GET})
    public @ResponseBody BaseResult addAdminTree(HttpServletRequest request){
        String id=request.getParameter("id");
        String level=request.getParameter("level");
        if(level.equals("5")){
            AdminAreaVo vo=(AdminAreaVo)adminAreaService.getAreaInfoByAreaId(id).getData();
            String zhenName=vo.getAreaName();
            AdminAreaVo vodi=(AdminAreaVo)adminAreaService.getParentAreaByAreaId(vo.getAreaId()).getData();
            String xianName=vodi.getAreaName();
            AdminAreaVo voci=(AdminAreaVo)adminAreaService.getParentAreaByAreaId(vodi.getAreaId()).getData();
            String shiName=voci.getAreaName();
            AdminAreaVo vopr=(AdminAreaVo)adminAreaService.getParentAreaByAreaId(voci.getAreaId()).getData();
            String shengName=vopr.getAreaName();
            AdminAreaVo voco=(AdminAreaVo)adminAreaService.getParentAreaByAreaId(vopr.getAreaId()).getData();
            String gouName=voco.getAreaName();
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("zhenName", zhenName);
            map.put("xianName", xianName);
            map.put("shiName", shiName);
            map.put("shengName", shengName);
            map.put("gouName", gouName);
            return BaseResult.success(map);
        }
        if(level.equals("4")){
            AdminAreaVo vo=(AdminAreaVo)adminAreaService.getAreaInfoByAreaId(id).getData();
            String xianName=vo.getAreaName();
            AdminAreaVo voci=(AdminAreaVo)adminAreaService.getParentAreaByAreaId(vo.getAreaId()).getData();
            String shiName=voci.getAreaName();
            AdminAreaVo vopr=(AdminAreaVo)adminAreaService.getParentAreaByAreaId(voci.getAreaId()).getData();
            String shengName=vopr.getAreaName();
            AdminAreaVo voco=(AdminAreaVo)adminAreaService.getParentAreaByAreaId(vopr.getAreaId()).getData();
            String gouName=voco.getAreaName();
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("xianName", xianName);
            map.put("shiName", shiName);
            map.put("shengName", shengName);
            map.put("gouName", gouName);
            return BaseResult.success(map);
        }
        if(level.equals("3")){
            //先查出本市名字
            logger.info("findAreaInfoByAreaId begin");
            AdminAreaVo vo=(AdminAreaVo)adminAreaService.getAreaInfoByAreaId(id).getData();
            logger.info("findAreaInfoByAreaId end");
            String shiName=vo.getAreaName();
            //再查出其父级，省的名字
            logger.info("findParentAreaByAreaId begin");
            AdminAreaVo vop=(AdminAreaVo)adminAreaService.getParentAreaByAreaId(id).getData();
            logger.info("findParentAreaByAreaId end");
            String shengName=vop.getAreaName();
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("shengName", shengName);
            map.put("shiName", shiName);
            return BaseResult.success(map);
        }
        if(level.equals("2")){
            //只查出本省的名字即可
            logger.info("findAreaInfoByAreaId begin");
            AdminAreaVo vo=(AdminAreaVo)adminAreaService.getAreaInfoByAreaId(id).getData();
            logger.info("findAreaInfoByAreaId end");
            String shengName=vo.getAreaName();
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("shengName", shengName);
            return BaseResult.success(map);
        }
        if(level.equals("1")){
            return BaseResult.success();
        }
        return null;
    }

    @RequestMapping(value={"/addAdminTreeStep2"},method={RequestMethod.POST,RequestMethod.GET})
    public @ResponseBody BaseResult addAdminTreeStap2(HttpServletRequest request,AdminAreaVo vo){
        String parentId=request.getParameter("id");
        String parentLevel=request.getParameter("level");
        Long level= Long.parseLong(parentLevel);
        String username = request.getParameter("username");
        String areaId=StringUtil.getUUID();
        vo.setAreaId(areaId);
        vo.setParentId(parentId);
        vo.setLevel(level+1);
        vo.setAddUser(username);
        logger.info("addAreaInfo begin");
        int result=(int)adminAreaService.addAreaInfo(vo).getData();
        logger.info("addAreaInfo end");
        if(result<0){
            return BaseResult.fail(ErrorCode.SQLExceptionRetCode, ErrorCode.SQLExceptionRetInfo);
        }
        return BaseResult.success();
    }

    @RequestMapping(value={"/editAdminTreeStep1"},method={RequestMethod.POST,RequestMethod.GET})
    public @ResponseBody BaseResult editAdminTreeStap1(HttpServletRequest request){
        String id=request.getParameter("id");
        String level=request.getParameter("level");
        if(level.equals("6")){
            AdminAreaVo vocu=(AdminAreaVo)adminAreaService.getAreaInfoByAreaId(id).getData();
            String cunName=vocu.getAreaName();
            AdminAreaVo vozh=(AdminAreaVo)adminAreaService.getParentAreaByAreaId(vocu.getAreaId()).getData();
            String zhenName=vozh.getAreaName();
            AdminAreaVo vodi=(AdminAreaVo)adminAreaService.getParentAreaByAreaId(vozh.getAreaId()).getData();
            String xianName=vodi.getAreaName();
            AdminAreaVo voci=(AdminAreaVo)adminAreaService.getParentAreaByAreaId(vodi.getAreaId()).getData();
            String shiName=voci.getAreaName();
            AdminAreaVo vopr=(AdminAreaVo)adminAreaService.getParentAreaByAreaId(voci.getAreaId()).getData();
            String shengName=vopr.getAreaName();
            AdminAreaVo voco=(AdminAreaVo)adminAreaService.getParentAreaByAreaId(vopr.getAreaId()).getData();
            String gouName=voco.getAreaName();
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("cunName", cunName);
            map.put("zhenName", zhenName);
            map.put("xianName", xianName);
            map.put("shiName", shiName);
            map.put("shengName", shengName);
            map.put("gouName", gouName);
            return BaseResult.success(map);
        }
        if(level.equals("5")){
            AdminAreaVo vo=(AdminAreaVo)adminAreaService.getAreaInfoByAreaId(id).getData();
            String zhenName=vo.getAreaName();
            AdminAreaVo vodi=(AdminAreaVo)adminAreaService.getParentAreaByAreaId(vo.getAreaId()).getData();
            String xianName=vodi.getAreaName();
            AdminAreaVo voci=(AdminAreaVo)adminAreaService.getParentAreaByAreaId(vodi.getAreaId()).getData();
            String shiName=voci.getAreaName();
            AdminAreaVo vopr=(AdminAreaVo)adminAreaService.getParentAreaByAreaId(voci.getAreaId()).getData();
            String shengName=vopr.getAreaName();
            AdminAreaVo voco=(AdminAreaVo)adminAreaService.getParentAreaByAreaId(vopr.getAreaId()).getData();
            String gouName=voco.getAreaName();
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("zhenName", zhenName);
            map.put("xianName", xianName);
            map.put("shiName", shiName);
            map.put("shengName", shengName);
            map.put("gouName", gouName);
            return BaseResult.success(map);
        }
        if(level.equals("4")){
            AdminAreaVo vo=(AdminAreaVo)adminAreaService.getAreaInfoByAreaId(id).getData();
            String xianName=vo.getAreaName();
            AdminAreaVo voci=(AdminAreaVo)adminAreaService.getParentAreaByAreaId(vo.getAreaId()).getData();
            String shiName=voci.getAreaName();
            AdminAreaVo vopr=(AdminAreaVo)adminAreaService.getParentAreaByAreaId(voci.getAreaId()).getData();
            String shengName=vopr.getAreaName();
            AdminAreaVo voco=(AdminAreaVo)adminAreaService.getParentAreaByAreaId(vopr.getAreaId()).getData();
            String gouName=voco.getAreaName();
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("xianName", xianName);
            map.put("shiName", shiName);
            map.put("shengName", shengName);
            map.put("gouName", gouName);
            return BaseResult.success(map);
        }
        if(level.equals("3")){
            //先查出本市名字
            logger.info("findAreaInfoByAreaId begin");
            AdminAreaVo vo=(AdminAreaVo)adminAreaService.getAreaInfoByAreaId(id).getData();
            logger.info("findAreaInfoByAreaId end");
            String shiName=vo.getAreaName();
            //再查出其父级，省的名字
            logger.info("findParentAreaByAreaId begin");
            AdminAreaVo vop=(AdminAreaVo)adminAreaService.getParentAreaByAreaId(id).getData();
            logger.info("findParentAreaByAreaId end");
            String shengName=vop.getAreaName();
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("shengName", shengName);
            map.put("shiName", shiName);
            return BaseResult.success(map);
        }
        if(level.equals("2")){
            //只查出本省的名字即可
            logger.info("findAreaInfoByAreaId begin");
            AdminAreaVo vo=(AdminAreaVo)adminAreaService.getAreaInfoByAreaId(id).getData();
            logger.info("findAreaInfoByAreaId end");
            String shengName=vo.getAreaName();
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("shengName", shengName);
            return BaseResult.success(map);
        }
        if(level.equals("1")){
            return BaseResult.success();
        }
        return null;
    }

    @RequestMapping(value={"/editAdminTreeStep2"},method={RequestMethod.POST,RequestMethod.GET})
    public @ResponseBody BaseResult editAdminTreeStap2(HttpServletRequest request,AdminAreaVo vo){
        String id=request.getParameter("id");
        String Stringlevel=request.getParameter("level");
        Long level= Long.parseLong(Stringlevel);
        String username = request.getParameter("username");
        vo.setAreaId(id);
        vo.setLevel(level);
        vo.setDelFlag("1");
        vo.setModifyUser(username);
        logger.info("updateAreaInfo begin");
        int result=(int)adminAreaService.editAreaInfo(vo).getData();
        logger.info("updateAreaInfo end");
        if(result<0){
            return BaseResult.fail(ErrorCode.SQLExceptionRetCode, ErrorCode.SQLExceptionRetInfo);
        }
        return BaseResult.success();
    }

    /**
     * @Description:三级联动-查询省(adminArea)
     * @return BaseResult
     
     * @Date:2016年4月4日
     * @Version 1.0.0
     */
    @RequestMapping(value="/getProvince",method={RequestMethod.POST,RequestMethod.GET})
    public @ResponseBody BaseResult getProvince(){
        logger.info("AreaRestController getProvince begin.");
        try{
            logger.info("getByLevel begin.");
            BaseResult baseResult=adminAreaService.getByLevel(PROVINCE_LEVEL);
            logger.info("getByLevel end.");
            logger.info("AreaRestController getProvince end.");
            return baseResult;
        }catch(Exception e){
            logger.error("getByLevel error:"+e.getMessage());
            logger.info("AreaRestController getProvince end.");
            return BaseResult.exception(e.getMessage());
        }
    }

    /**
     * @Description:三级联动-查询市/县(adminArea)
     * @param request
     * @return BaseResult
     
     * @Date:2016年4月4日
     * @Version 1.0.0
     */

    @RequestMapping(value="/getByParent",method={RequestMethod.POST,RequestMethod.GET})
    public @ResponseBody BaseResult getByParent(HttpServletRequest request){
        String parentId=request.getParameter("parentId");
        logger.info("AreaRestController queryByParent begin.");
        try{
            logger.info("queryByParentId begin.");
            BaseResult baseResult=adminAreaService.getByParentId(parentId);
            logger.info("queryByParentId end.");
            logger.info("AreaRestController queryByParent end.");
            return baseResult;
        }catch(Exception e){
            logger.error("queryByParentId error:"+e.getMessage());
            logger.info("AreaRestController queryByParent end.");
            return BaseResult.exception(e.getMessage());
        }
    }

    /**
     * @Description:校验区域编码是否重复
     * @param request
     * @return BaseResult
     
     * @Date:2016年4月10日
     * @Version 1.0.0
     */

    @RequestMapping(value="/isExistAdminAreaCode",method={RequestMethod.POST,RequestMethod.GET})
    public @ResponseBody BaseResult isExistAreaCode(HttpServletRequest request){
        String id=request.getParameter("id");
        String areaCode=request.getParameter("areaCode");
        logger.info("AreaRestController isExistAreaCode begin.");
        try{
            logger.info("isExistAreaCode begin.");
            BaseResult baseResult=adminAreaService.isExistAreaCode(id,areaCode);
            logger.info("isExistAreaCode end.");
            logger.info("AreaRestController isExistAreaCode end.");
            return baseResult;
        }catch(Exception e){
            logger.error("isExistAreaCode error:"+e.getMessage());
            logger.info("AreaRestController isExistAreaCode end.");
            return BaseResult.exception(e.getMessage());
        }
    }
}
